package com.nuanshui.framework.behavior;

import com.nuanshui.framework.lang.ClassUtil;
import com.nuanshui.framework.springContext.SpringContextUtil;

import java.util.*;

/**
 * Created by zxh on 2017/1/6.
 */
public class BehaviorUtil {

    public static <T> List<T> getBehaviorList(String packageName)
    {

        Set<Class<?>> classFeatures = ClassUtil.getAnnotationClasses(BehaviorAttribute.class, packageName);

        List<T> list=new ArrayList<T>();
        for (Class<?> classItem:classFeatures) {
            T item=(T) SpringContextUtil.getBean(classItem);
            list.add(item);
        }
        return list;
    }

    public static <T> Map<Integer, T> getBehaviorMap(String packageName) {
        Map<Integer, T> maps = new HashMap<Integer, T>();
        Set<Class<?>> classFeatures = ClassUtil.getAnnotationClasses(BehaviorAttribute.class, packageName);
        for (Class<?> cls : classFeatures) {
            BehaviorAttribute item = cls.getAnnotation(BehaviorAttribute.class);
            if (item != null) {
                if (item.id() > -1)
                    maps.put(item.id(), (T) SpringContextUtil.getBean(cls));
            }
        }

        return maps;
    }
}
